<?php
namespace manage;

require_once __DIR__ . '/Lib/Base.php';
require_once __DIR__ . '/Lib/Page.php';

use Workerman\Protocols\Http;
if(!Http::sessionStarted()) Http::sessionStart();
checkLogin();

// 权限检查
$funcs = getFuncs($_SESSION['admin_group']);
if(!in_array(9, $funcs)) {
	echo '<script type="text/javascript">location.href="dashboard.php";</script>';
	#Http::header('Location:dashboard.php');
	return;
}

global $plat_db;

$error = $success = NULL;
$now = time();
$kc = KEY['notice'];

if(isset($_POST['type'])) {
    if(!$_POST['type'] || !$_POST['title'] || !$_POST['content'] || !$_POST['start_time'] || !$_POST['end_time']) {
        $error = '信息不完整,请重新填写';
    } else if(strtotime($_POST['start_time']) >= strtotime($_POST['end_time'])) {
        $error = '开始时间不能大于结束时间';
    } else {
	    $new_notice_id = null;
	    try {
	        if(!$_POST['banner']) $banner = '';
	        else $banner = $_POST['banner'];

	        if(!$_POST['btn_x']) $btn_x = 0;
	        else $btn_x = $_POST['btn_x'];

	        if(!$_POST['btn_y']) $btn_y = 0;
	        else $btn_y = $_POST['btn_y'];

	        if(!$_POST['btn_title']) $btn_title = '';
	        else $btn_title = $_POST['btn_title'];

	        if(!$_POST['jump']) $jump = '';
	        else $jump = $_POST['jump'];

		    $new_notice = [
			    'type' => $_POST['type'],
			    'title' => $_POST['title'],
			    'content' => $_POST['content'],
			    'banner'  => $banner,
			    'create_time' => $now,
			    'start_time' => strtotime($_POST['start_time']),
			    'end_time' => strtotime($_POST['end_time']),
			    'sort' => $_POST['sort'],
			    'btn_x' => $btn_x,
			    'btn_y' => $btn_y,
			    'btn_title' => $btn_title,
			    'jump' => $jump,
			    'status' => 1,
		    ];

		    $notice_keys = \Core\Cache::hkeys($kc['instance'], $kc['pre']);
		    if(empty($notice_keys)) $max_id = 0;
		    else $max_id = max($notice_keys);
		    \Core\Cache::hset($kc['instance'], $kc['pre'], ($max_id+1), $new_notice, $kc['encode']);
	    } catch (\Exception $e) {
		    $error = '添加公告失败: ' . $e->getMessage();
	    }
	    if(!$error) {
	        $success = '添加公告成功';
		    operate_log('notice', 'add', [
			    $new_notice_id, $_POST['type'], $_POST['title'], $_POST['content'], $_POST['start_time'], $_POST['end_time'], $_POST['sort']
            ]);
	    }
    }
} else if(isset($_GET['action'])) {
    $action = $_GET['action'];
	if($action == 'pulloff') {
		$nid = @intval($_GET['id']);
		if(!$nid) {
			$error = '无效的公告ID';
		} else {
			try {
				$ninfo = \Core\Cache::hget($kc['instance'], $kc['pre'], $nid, $kc['encode']);
				$ninfo['status'] = 0;
				\Core\Cache::hset($kc['instance'], $kc['pre'], $nid, $ninfo, $kc['encode']);
			} catch(\Exception $e) {
				$error = '下架失败, ' . $e->getMessage();
			}
			if(!$error) {
			    $success = '下架成功';
				operate_log('notice', 'pulloff', [$nid]);
			}
		}
	}
	else if($action == 'pushon') {
		$nid = @intval($_GET['id']);
		if(!$nid) {
			$error = '无效的公告ID';
		} else {
			try {
				$ninfo = \Core\Cache::hget($kc['instance'], $kc['pre'], $nid, $kc['encode']);
				$ninfo['status'] = 1;
				\Core\Cache::hset($kc['instance'], $kc['pre'], $nid, $ninfo, $kc['encode']);
			} catch(\Exception $e) {
				$error = '上架失败, ' . $e->getMessage();
			}
			if(!$error) {
			    $success = '上架成功';
				operate_log('notice', 'pushon', [$nid]);
			}
		}
	}
	else if($action == 'delete') {
		$nid = @intval($_GET['id']);
		if(!$nid) {
			$error = '无效的公告ID';
		} else {
			try {
				\Core\Cache::hdel($kc['instance'], $kc['pre'], $nid, $kc['encode']);
			} catch(\Exception $e) {
				$error = '删除失败, ' . $e->getMessage();
			}
			if(!$error) {
			    $success = '删除成功';
				operate_log('notice', 'delete', [$nid]);
			}
		}
	} else if($action == 'edit') {
		$nid = @intval($_POST['nid']);
		if(!$nid) {
			$error = '无效的公告ID';
		} else if(!$_POST['edittype'] || !$_POST['title'] || !$_POST['content'] || !$_POST['start_time'] || !$_POST['end_time']) {
			$error = '信息不完整,请重新填写';
		} else if(strtotime($_POST['start_time']) >= strtotime($_POST['end_time'])) {
			$error = '开始时间不能大于结束时间';
		} else {
			try {
			    if(!$_POST['banner']) $banner = '';
			    else $banner = $_POST['banner'];

			    if(!$_POST['btn_x']) $btn_x = 0;
		        else $btn_x = $_POST['btn_x'];

		        if(!$_POST['btn_y']) $btn_y = 0;
		        else $btn_y = $_POST['btn_y'];

		        if(!$_POST['btn_title']) $btn_title = '';
		        else $btn_title = $_POST['btn_title'];

		        if(!$_POST['jump']) $jump = '';
		        else $jump = $_POST['jump'];


				$ninfo = [
					'type' => $_POST['edittype'],
					'title' => $_POST['title'],
					'content' => $_POST['content'],
					'banner'  => $banner,
					'create_time' => $now,
					'start_time' => strtotime($_POST['start_time']),
					'end_time' => strtotime($_POST['end_time']),
					'sort' => $_POST['sort'],
					'btn_x' => $btn_x,
				    'btn_y' => $btn_y,
				    'btn_title' => $btn_title,
				    'jump' => $jump,
				];

				\Core\Cache::hset($kc['instance'], $kc['pre'], $nid, $ninfo, $kc['encode']);
			} catch (\Exception $e) {
				$error = '修改公告失败: ' . $e->getMessage();
			}
			if(!$error) {
			    $success = '修改公告成功';
				operate_log('notice', 'edit', [
					$nid, $_POST['edittype'], $_POST['title'], $_POST['content'], $_POST['start_time'], $_POST['end_time'], 
					$_POST['sort'],$_POST['btn_x'],$_POST['btn_y'],$_POST['btn_title'],$_POST['jump'],
				]);
			}
		}
    }
}

foreach($_GET as $k => $v) { unset($_GET[$k]); }
foreach($_POST as $k => $v) { unset($_POST[$k]); }

$notice_list = \Core\Cache::hgetall($kc['instance'], $kc['pre']) ?? [];

//获取operation_log最后同步时间
//global $manage_db;
//$last_sync = $manage_db->select('time')->from('operation_log')->where("module='notice' and action='sync'")->orderByDesc(['id'])->row();
//if(!isset($last_sync['time'])) {
//    $last_sync_time = '-';
//} else {
//    $last_sync_time = date('Y-m-d H:i:s', $last_sync['time']);
//}

display_header();
?>

<section class="content">

	<?php display_title('公告管理'); ?>

	<?php if($error) : ?>
		<div class="alert alert-warning alert-dismissible">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
			<?php echo $error; ?>
		</div>
	<?php endif ?>
	<?php if($success) : ?>
		<div class="alert alert-success alert-dismissible">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
			<h4><i class="icon fa fa-check"></i> 成功</h4>
			<?php echo $success; ?>
		</div>
	<?php endif ?>

	<div class="box box-primary">
		<div class="box-header with-border">
			<h3 class="box-title"><i class="fa fa-search"></i> 公告列表</h3>
		</div>

		<div class="box-body">

            <button type="button" class="btn btn-primary pull-left" style="margin-bottom:10px;" data-toggle="modal" data-target="#modal-add-notice">添加公告</button>

			<table id="example1" class="table table-bordered table-striped dataTable table-hover" role="grid" aria-describedby="example1_info">
				<thead>
				<tr>
					<th class="" rowspan="1" colspan="1" width="50" style="text-align:center">ID<hr />排序</th>
					<th class="" rowspan="1" colspan="1" width="50" style="text-align:center;">类型</th>
                    <th class="" rowspan="1" colspan="1" width="50" style="text-align:center;">配图</th>
					<th class="" rowspan="1" colspan="1">标题</th>
					<th class="" rowspan="1" colspan="1">内容</th>
					<th class="" rowspan="1" colspan="1" width="100" style="text-align:center">创建/修改<br />时间</th>
					<th class="" rowspan="1" colspan="1" width="100" style="text-align:center">开始时间</th>
					<th class="" rowspan="1" colspan="1" width="100" style="text-align:center">结束时间</th>
                    <th class="" rowspan="1" colspan="1" width="70" style="text-align:center">状态</th>
                    <th class="" rowspan="1" colspan="1" width="70" style="text-align:center">按钮X</th>
                    <th class="" rowspan="1" colspan="1" width="70" style="text-align:center">按钮Y</th>
                    <th class="" rowspan="1" colspan="1" width="70" style="text-align:center">按钮文字</th>
                    <th class="" rowspan="1" colspan="1" width="70" style="text-align:center">执行代码</th>
					<th class="" rowspan="1" colspan="1" width="180">操作</th>
				</tr>
				</thead>
				<tbody>
				<?php if(!empty($notice_list)) { foreach($notice_list as $nid => $n) { ?>
					<tr role="row" class="odd">
						<td style="vertical-align:middle;text-align:center;"><?php echo $nid; ?><hr /><?php echo $n['sort']; ?></td>
						<td style="vertical-align:middle;text-align:center;"><?php echo $n['type']=='activity' ? '活动' : '系统'; ?></td>
                        <td style="vertical-align:middle;text-align:center;"><?php if($n['banner']) echo '<button banner_src="'.$n['banner'].'" type="button" class="btn btn-sm btn-info pull-left open_banner" data-toggle="modal" data-target="#modal-open-banner">查看</button>'; else echo '无'; ?></td>
						<td style="vertical-align:middle;"><?php echo $n['title']; ?></td>
                        <td style="vertical-align:middle;"><textarea readonly style="width:100%;border:0;background:none;outline:none;" rows="4" class="form-control"><?php echo $n['content']; ?></textarea></td>
						<td style="vertical-align:middle;font-size:10px;text-align:center;"><?php echo date('Y-m-d H:i:s', $n['create_time']); ?></td>
                        <td style="vertical-align:middle;font-size:10px;text-align:center;"><?php
                            if($n['start_time'] > $now) echo '<span class="orange strong">'; else echo '<span class="green">';
                            echo date('Y-m-d H:i:s', $n['start_time']).'</span>';
                            ?></td>
                        <td style="vertical-align:middle;font-size:10px;text-align:center;"><?php
	                        if($n['end_time'] < $now) echo '<span class="orange strong">'; else echo '<span class="green">';
	                        echo date('Y-m-d H:i:s', $n['end_time']).'</span>';
	                        ?></td>
						<td style="vertical-align:middle;text-align:center;"><?php echo $n['status']==0 ? '<span class="red strong">下架</span>' : ($n['status']==1 ? '<span class="green strong">正常</span>' : '无效状态'); ?></td>
						<td style="vertical-align:middle;"><?php echo $n['btn_x']; ?></td>
						<td style="vertical-align:middle;"><?php echo $n['btn_y']; ?></td>
						<td style="vertical-align:middle;"><?php echo $n['btn_title']; ?></td>
						<td style="vertical-align:middle;"><?php echo $n['jump']; ?></td>
						<td style="vertical-align:middle; nid="<?php echo trim($nid); ?>">
							<button data_id="<?php echo $nid; ?>" data_banner="<?php echo $n['banner']; ?>" data_type="<?php echo $n['type']; ?>" data_sort="<?php echo $n['sort']; ?>" type="button" class="btn btn-sm btn-success pull-left" data-toggle="modal" data-target="#modal-edit-notice" style="margin-right:8px;margin-bottom:8px;">编辑</button>
                            <?php if($n['status'] == 1) { ?>
                                <button type="button" class="btn btn-sm btn-warning pull-left pulloff_notice" style="margin-right:8px;margin-bottom:8px;">下架</button>
                            <?php } else { ?>
                                <button type="button" class="btn btn-sm btn-success pull-left pushon_notice" style="margin-right:8px;">上架</button>
                            <?php } ?>
							<button type="button" class="btn btn-sm btn-danger pull-left del_notice">删除</button>
						</td>
					</tr>
				<?php } } ?>
				</tbody>
			</table>
<!--			<div class="row" style="margin-top:15px;">-->
<!--				<div class="col-sm-4"><div class="dataTables_info" id="example1_info" style="line-height:32px;">Showing 1 to 10 of 57 entries</div></div>-->
<!--				<div class="col-sm-8"><div class="" id="example1_paginate"><ul style="margin:0;" class="pagination"><li class="paginate_button previous disabled" id="example1_previous"><a href="#" aria-controls="example1" data-dt-idx="0" tabindex="0">Previous</a></li><li class="paginate_button active"><a href="#" aria-controls="example1" data-dt-idx="1" tabindex="0">1</a></li><li class="paginate_button "><a href="#" aria-controls="example1" data-dt-idx="2" tabindex="0">2</a></li><li class="paginate_button "><a href="#" aria-controls="example1" data-dt-idx="3" tabindex="0">3</a></li><li class="paginate_button "><a href="#" aria-controls="example1" data-dt-idx="4" tabindex="0">4</a></li><li class="paginate_button "><a href="#" aria-controls="example1" data-dt-idx="5" tabindex="0">5</a></li><li class="paginate_button "><a href="#" aria-controls="example1" data-dt-idx="6" tabindex="0">6</a></li><li class="paginate_button next" id="example1_next"><a href="#" aria-controls="example1" data-dt-idx="7" tabindex="0">Next</a></li></ul></div></div>-->
<!--			</div>-->
		</div>
	</div>

    <div class="modal fade" id="modal-open-banner">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">查看公告配图</h4>
                </div>
                <div class="modal-body">
                    <img src="" style="max-width:850px;" />
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="sync_server_notice">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">同步线上公告</h4>
                </div>
                <div class="modal-body">
                    确认同步线上服务器的公告内容吗?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="sync_notice_btn">确定</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="modal-add-notice">
        <form action="notice.php" method="post" id="add_notice_form">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">添加公告</h4>
                </div>
                <div class="modal-body">
                    <table class="table table-bordered table-striped dataTable table-hover">
                        <tr>
                            <th>类型</th>
                            <td>
                                <label>
                                    <input required type="radio" name="type" class="minimal" value="activity" checked />
                                    活动
                                </label>
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <label>
                                    <input required type="radio" name="type" class="minimal" value="system" />
                                    系统
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <th>标题</th>
                            <td><input required class="form-control" type="text" name="title" required></td>
                        </tr>
                        <tr>
                            <th>正文</th>
                            <td><textarea required class="form-control" rows="6" name="content" required></textarea></td>
                        </tr>
                        <tr>
                            <th>配图<br />最大尺寸:606*320</th>
                            <td><input required class="form-control" id="add_banner" type="text" name="banner"></td>
                        </tr>
                        <tr>
                            <th>配图预览</th>
                            <td><img id="add_banner_lookup" src="" style="max-width:500px;max-height:200px;"></td>
                        </tr>
                        <tr>
                            <th>开始时间</th>
                            <td>
                                <div class="input-group date">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <input required type="text" autocomplete="off" class="form-control pull-right" id="datepicker1" name="start_time" placeholder="点击选择开始时间">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <th>结束时间</th>
                            <td>
                                <div class="input-group date">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <input required type="text" autocomplete="off" class="form-control pull-right" id="datepicker2" name="end_time" placeholder="点击选择结束时间">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <th>排序</th>
                            <td><input required class="form-control" type="text" placeholder="0排最上" name="sort"></td>
                        </tr>
						

						<tr>
                            <th>按钮X轴</th>
                            <td><input required class="form-control" type="text"  name="btn_x"></td>
                        </tr>

                        <tr>
                            <th>按钮Y轴</th>
                            <td><input required class="form-control" type="text"  name="btn_y"></td>
                        </tr>

                        <tr>
                            <th>按钮文字</th>
                            <td><input required class="form-control" type="text"  name="btn_title"></td>
                        </tr>

                        <tr>
                            <th>按钮执行代码</th>
                            <td><input required class="form-control" type="text"  name="jump"></td>
                        </tr>


                    </table>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="add_notice_submit">确定</button>
                </div>
            </div>
        </div>
        </form>
    </div>

    <div class="modal fade" id="modal-edit-notice">
        <form action="notice.php?action=edit" method="post" id="edit_notice_form">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">编辑公告</h4>
                    </div>
                    <div class="modal-body">
                        <table class="table table-bordered table-striped dataTable table-hover">
                            <tr>
                                <th width="150">类型</th>
                                <td>
                                    <select required class="form-control data_type" name="edittype">
                                        <option value="activity">活动</option>
                                        <option value="system">系统</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <th>标题</th>
                                <td><input required class="form-control edit_input_title" type="text" name="title"></td>
                            </tr>
                            <tr>
                                <th>正文</th>
                                <td><textarea required class="form-control edit_input_content" rows="6" name="content"></textarea></td>
                            </tr>
                            <tr>
                                <th>配图<br />最大尺寸:606*320</th>
                                <td><input required class="form-control edit_input_banner" type="text" name="banner"></td>
                            </tr>
                            <tr>
                                <th>配图预览</th>
                                <td><img id="edit_banner_lookup" src="" style="max-width:500px;max-height:200px;"></td>
                            </tr>
                            <tr>
                                <th>开始时间</th>
                                <td>
                                    <div class="input-group date">
                                        <div class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </div>
                                        <input required autocomplete="off" type="text" class="form-control pull-right edit_input_start_time" id="datepicker1" name="start_time" placeholder="点击选择开始时间">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <th>结束时间</th>
                                <td>
                                    <div class="input-group date">
                                        <div class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </div>
                                        <input required autocomplete="off" type="text" class="form-control pull-right edit_input_end_time" id="datepicker2" name="end_time" placeholder="点击选择结束时间">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <th>排序</th>
                                <td><input required class="form-control edit_input_sort" type="text" placeholder="0排最上" name="sort"></td>
                            </tr>

							
							<tr>
	                            <th>按钮X轴</th>
	                            <td><input required class="form-control edit_input_btnx" type="text"  name="btn_x"></td>
	                        </tr>

	                        <tr>
	                            <th>按钮Y轴</th>
	                            <td><input required class="form-control edit_input_btny" type="text"  name="btn_y"></td>
	                        </tr>

	                        <tr>
	                            <th>按钮文字</th>
	                            <td><input required class="form-control edit_input_btntitle" type="text"  name="btn_title"></td>
	                        </tr>

	                        <tr>
	                            <th>按钮执行代码</th>
	                            <td><input required class="form-control edit_input_jump" type="text"  name="jump"></td>
	                        </tr>

                        </table>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" id="edit_notice_submit">确定</button>
                        <input type="hidden" class="this_notice_id" name="nid" value="" />
                    </div>
                </div>
            </div>
        </form>
    </div>

</section>

<!-- jQuery 3 -->
<script src="http://gold-static.91changwan.com/web/manage_static/js/jquery.min.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/js/jquery.cookie.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/js/jquery.pjax.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Select2 -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/select2/dist/js/select2.full.min.js"></script>
<!-- InputMask -->
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/input-mask/jquery.inputmask.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/input-mask/jquery.inputmask.extensions.js"></script>
<!-- date-range-picker -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/moment/min/moment.min.js"></script>
<!-- bootstrap datepicker -->
<script src="http://gold-static.91changwan.com/web/manage_static/js/bootstrap-datetimepicker.min.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/js/bootstrap-datepicker-zhcn.min.js"></script>
<!-- bootstrap time picker -->
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/timepicker/bootstrap-timepicker.min.js"></script>
<!-- SlimScroll -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<!-- iCheck 1.0.1 -->
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/iCheck/icheck.min.js"></script>
<!-- FastClick -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/fastclick/lib/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="http://gold-static.91changwan.com/web/manage_static/dist/js/adminlte.min.js"></script>
<script type="text/javascript">
    $(function() {
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass   : 'iradio_minimal-blue'
        })
        //Red color scheme for iCheck
        $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
            checkboxClass: 'icheckbox_minimal-red',
            radioClass   : 'iradio_minimal-red'
        })
        $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
            checkboxClass: 'icheckbox_flat-green',
            radioClass   : 'iradio_flat-green'
        });

        $('#datepicker1').datetimepicker(
            {
                language: 'zh-CN',
                autoclose: true,
                timePicker: true,
                todayHighlight: true,
                todayBtn: true,
                weekStart: 0,
                format: 'yyyy-mm-dd hh:ii:ss'
            },
        );
        $('#datepicker2').datetimepicker(
            {
                language: 'zh-CN',
                autoclose: true,
                timePicker: true,
                todayHighlight: true,
                todayBtn: true,
                weekStart: 0,
                format: 'yyyy-mm-dd hh:ii:ss'
            },
        );
        $('#add_notice_submit').click(function() {
           $('#add_notice_form').submit();
        });
        $('#sync_notice_btn').click(function() {
        	$.ajax({
                url: 'player_action.php?action=send_noitce',
                beforeSend: function() {
                },
                success: function(data) {
                }
            });
           location.href = 'notice.php?refresh=1';
        });
        //下架
        $('.pulloff_notice').click(function() {
           var nid = $(this).prev().attr('data_id');
           location.href='notice.php?action=pulloff&id=' + nid;
        });
        //上架
        $('.pushon_notice').click(function() {
            var nid = $(this).prev().attr('data_id');
            location.href='notice.php?action=pushon&id=' + nid;
        });
        //删除
        $('.del_notice').click(function() {
            if(confirm('确认删除这条公告吗?\n删除后不可恢复, 如果希望暂时不显示, 可以先下架.')) {
                var nid = $(this).prev().prev().attr('data_id');
                location.href = 'notice.php?action=delete&id=' + nid;
            }
        });
        $('#modal-open-banner').on('show.bs.modal', function (e) {
            var modal = $(this);
            modal.find('.modal-body img').attr('src', '');
            var btn = $(e.relatedTarget);
            var src = $(btn).attr('banner_src');
            modal.find('.modal-body img').attr('src', src);
        });
        $('#modal-edit-notice').on('show.bs.modal', function (e) {
            var btn = $(e.relatedTarget);
            var type = $(btn).attr('data_type');
            var title = $(btn).parent().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().text();
            var content = $(btn).parent().prev().prev().prev().prev().prev().prev().prev().prev().prev().text();
            var banner = $(btn).attr('data_banner');
            var start_time = $(btn).parent().prev().prev().prev().prev().prev().prev().prev().text();
            var end_time = $(btn).parent().prev().prev().prev().prev().prev().prev().text();
            var sort = $(btn).attr('data_sort');

			var btn_x = $(btn).parent().prev().prev().prev().prev().text();
			var btn_y = $(btn).parent().prev().prev().prev().text();
			var btn_title = $(btn).parent().prev().prev().text();
			var jump = $(btn).parent().prev().text();

            var nid = $(btn).parent().attr('nid');
            nid = $(btn).attr('data_id');
            var modal = $(this);
            modal.find('.edit_input_title').val(title);
            modal.find('.edit_input_content').val(content);
            modal.find('.edit_input_banner').val(banner);
            modal.find(".data_type").val(type);
            modal.find('.edit_input_sort').val(sort);
            modal.find('.edit_input_start_time').val(start_time);
            modal.find('.edit_input_end_time').val(end_time);
            modal.find('.this_notice_id').val(nid);
		
			modal.find('.edit_input_btnx').val(btn_x);
			modal.find('.edit_input_btny').val(btn_y);
			modal.find('.edit_input_btntitle').val(btn_title);
			modal.find('.edit_input_jump').val(jump);


            if(banner) {
                $('#edit_banner_lookup').attr('src', banner);
            }
        });
        $('#add_banner').blur(function() {
           $('#add_banner_lookup').attr('src', $(this).val());
        });
        $('#modal-edit-notice .edit_input_banner').blur(function() {
           $('#edit_banner_lookup').attr('src', $(this).val());
        });

        $('#edit_notice_submit').click(function() {
            $('#edit_notice_form').submit();
        });

        setTimeout(function() {
            $('.alert-success').fadeOut();
            if($('.alert-warning').find('span').html() && $('.alert-warning').find('span').html().length < 60) {
                $('.alert-warning').fadeOut();
            }
        }, 2000);
    });
</script>

</body>
</html>
